/******************************************************
 *                jClass implementation
 ******************************************************/

window.jClass = function(body) {
    var tempObj = function() {};
    try {
        tempObj = jUtils.handleStatics(jUtils.objectToMethod(new (jClass.create(body, undefined))));
    } catch (ex) { throw(ex); }
    tempObj.prototype = new jClassId('jClass', undefined, [tempObj._typeId], []);
    return tempObj;
}

jClass.create = function(body, interfaceList) {
    var tempObj = function() {};
    if (body) {
        if (interfaceList) {
            jUtils.validateInterface(interfaceList, body, arguments[2])
        }
        body.isInstanceOf = jUtils.isInstanceOf;
        body.isImplementationOf = jUtils.isImplementationOf;
        tempObj = function() { return body; };
        tempObj.prototype = body.prototype;
        return tempObj;
    }
    return tempObj;
}

jClass.extend = function(baseClass) {
    var objectBody = {};
    this.ext = [];
    
    var tmp = function(body) {
        
        jUtils.mergeObjects(objectBody, body);
        var interfaceList = arguments[1];
        var inheritedStatics = {}; for (var ex in baseClass) { if (ex != 'prototype') inheritedStatics[ex] = baseClass[ex]; }
        var tempObj = jUtils.handleStatics(jUtils.objectToMethod(new (jClass.create(objectBody, interfaceList, baseClass)), baseClass), inheritedStatics);        
        var interfaces = new Array();
        if (interfaceList) {
            for (var i = 0; i < interfaceList.length; i++) {
                 interfaces.push(interfaceList[i]._typeId);
            }
        }

        if (typeof tempObj.prototype._prototype == 'undefined') {
            tempObj.prototype = new jClassId('jClass', undefined, [tempObj._typeId].concat(tempObj._extending), interfaces);
        } else {
            new jClassId('jClass', tempObj.prototype._prototype, [tempObj._typeId].concat(tempObj._extending), interfaces)
        }
        return tempObj;
    }

    tmp.implement = function() {
        var interfaces = arguments;
        var _parent = this;
        return function(body) {
            if (jUtils.getBody(baseClass) != 'function () {var END = "";}') {
                return _parent(body, interfaces);
            } else
            return _parent(body, interfaces);
        }
    }

    return tmp;
};

jClass.implement = function() {
    var interfaces = arguments;
    var blank = function() { var END = ''; }
    blank._typeId = "";
    var _parent = jClass.extend(blank);
    return function(body) {
        return _parent.implement.apply(_parent, interfaces)(body);// tmp.call(interfaces)(body);
    }
};
